Return to start page

Core/String/Library Misc.j

Code

		
1			library ALibraryCoreStringMisc requires optional ALibraryCoreDebugMisc
2
3 debug function StringPositionDebug takes string whichString, integer position returns nothing
4 debug if ((position < 0) or (position >= StringLength(whichString))) then
5 debug call Print("StringPositionDebug: Wrong position value: " + I2S(position) + ".")
6 debug endif
7 debug endfunction
8
9 /**
10 * Searches for position of string @param searchedString in string @param whichString.
11 * If @param searchedString is not contained by @param whichString function will return -1 otherwise it will return its position.
12 * @param whichString String which should contain searched string.
13 * @param searchedString String which is searched.
14 * @return If the string was found it returns its position otherwise it will return -1.
15 */
16 function FindString takes string whichString, string searchedString returns integer
17 local integer i
18 debug if (StringLength(whichString) < StringLength(searchedString)) then
19 debug call Print("FindString: Used string is lesser than searched string.")
20 debug endif
21 set i = 0
22 loop
23 exitwhen (i + StringLength(searchedString) > StringLength(whichString))
24 if (SubString(whichString, i, i + StringLength(searchedString)) == searchedString) then
25 return i
26 endif
27 set i = i + 1
28 endloop
29 return -1
30 endfunction
31
32 /**
33 * Replaces a part of a string and returns the resulting string.
34 * @param whichString String which is used for replacement operation.
35 * @param position Position where replacement should start.
36 * @param replacingString String which should replace a sub string of string @param whichString.
37 * @return Returns the new string with the replaced sub string.
38 */
39 function ReplaceSubString takes string whichString, integer position, string replacingString returns string
40 local string result = ""
41 debug call StringPositionDebug(whichString, position)
42 set result = SubString(whichString, 0, position) + replacingString
43 if (position + StringLength(replacingString) < StringLength(whichString)) then
44 set result = result + SubString(whichString, position + StringLength(replacingString), StringLength(whichString))
45 endif
46 return result
47 endfunction
48
49 /**
50 * Replaces string @param replacedString in string @param whichString by string @param replacingString and returns the resulting string.
51 * Note that @param replacedString and @replacingString do not have to have the same size. The function removes @param replacedString and inserts @param replacingString afterwards at @param replacedString's old position.
52 * @param whichString String which contains the sub string @param replacedString.
53 * @param replacedString Sub string of string @param whichString which should be replaced.
54 * @param replacingString String which should replace @param replacedString.
55 * @return Returns the new string with the replaced sub string.
56 */
57 function ReplaceString takes string whichString, string replacedString, string replacingString returns string
58 local integer position = FindString(whichString, replacedString)
59 if (position == -1) then
60 return whichString
61 endif
62 return SubString(whichString, 0, position) + replacingString + SubString(whichString, position + StringLength(replacedString), StringLength(whichString))
63 endfunction
64
65 /**
66 * Removes the sub string at position @param position with length @param length of string @param whichString and returns the resulting string.
67 * @param whichString String which is used for removing the sub string.
68 * @param position Position of the sub string which should be removed.
69 * @param length Length of the sub string which should be removed.
70 * @return Returns the new string without the sub string string.
71 */
72 function RemoveSubString takes string whichString, integer position, integer length returns string
73 debug call StringPositionDebug(whichString, position)
74 debug call StringPositionDebug(whichString, position + length - 1)
75 return SubString(whichString, 0, position) + SubString(whichString, position + length, StringLength(whichString))
76 endfunction
77
78 /**
79 * Removes string @param removedString from string @param whichString and returns the resulting string.
80 * @param whichString String from which @param subString should be removed.
81 * @param removedString String which should be removed.
82 * @return Returns the new string without the removed string.
83 */
84 function RemoveString takes string whichString, string removedString returns string
85 local integer position = FindString(whichString, removedString)
86 if (position == -1) then
87 debug call Print("RemoveString: String \"" + whichString + "\" does not contain string \"" + removedString + "\".")
88 return whichString
89 endif
90 return RemoveSubString(whichString, position, StringLength(removedString))
91 endfunction
92
93 /**
94 * Inserts string @param insertedString into string @param whichString at position @param position and returns the resulting string.
95 * @param whichString String into which string @param insertedString should be inserted.
96 * @param position Position where string @param insertedString should be inserted.
97 * @param insertedString String which should be inserted into string @param whichString at position @param position.
98 * @return Returns the new string with the inserted string.
99 */
100 function InsertString takes string whichString, integer position, string insertedString returns string
101 local string result = ""
102 debug call StringPositionDebug(whichString, position)
103 return SubString(whichString, 0, position) + insertedString + SubString(whichString, position, StringLength(whichString))
104 endfunction
105
106 /**
107 * Moves the sub string of string @param whichString at position @param position and with length @param length to position @param newPosition and returns the resulting string.
108 * @param whichString String in which the sub string should be moved.
109 * @param position Start position of the sub string.
110 * @param length Length of the sub string.
111 * @param newPosition Position to which the sub string should be moved.
112 * @return Returns the new string with the moved string.
113 */
114 function MoveSubString takes string whichString, integer position, integer length, integer newPosition returns string
115 local string subString = ""
116 local string result = ""
117 debug call StringPositionDebug(whichString, position)
118 debug call StringPositionDebug(whichString, position + length - 1)
119 debug call StringPositionDebug(whichString, newPosition)
120 debug call StringPositionDebug(whichString, newPosition + length - 1)
121 set subString = SubString(whichString, position, length)
122 set result = RemoveSubString(whichString, position, length)
123 return InsertString(whichString, newPosition + StringLength(whichString) - StringLength(result), subString)
124 endfunction
125
126 /**
127 * Moves string @param movedString of string @param whichString to position @param newPosition and returns the resulting string.
128 * @param whichString String in which string @param movedString should be moved.
129 * @param movedString String which should be moved.
130 * @param newPosition Position to which string @param movedString should be moved.
131 * @return Returns the new string with the moved string.
132 */
133 function MoveString takes string whichString, string movedString, integer newPosition returns string
134 local integer position = FindString(whichString, movedString)
135 if (position == -1) then
136 debug call Print("MoveString: String \"" + whichString + "\" does not contain string \"" + movedString + "\".")
137 return whichString
138 endif
139 return MoveSubString(whichString, position, StringLength(whichString), newPosition)
140 endfunction
141
142 /**
143 * Reverses a string that it will be written backwards and returns the resulting string.
144 * @param whichString String which should be reversed.
145 * @return Returns the new reversed string.
146 */
147 function ReverseString takes string whichString returns string
148 local integer i = StringLength(whichString)
149 local string result = ""
150 loop
151 exitwhen (i == 1)
152 set result = result + SubString(whichString, i - 1, i)
153 set i = i - 1
154 endloop
155 return result
156 endfunction
157
158 /**
159 * Basic case (in)sensitive pattern matching.
160 * Supported wildcard characters:
161 * * - matches 0 or more any characters
162 * ? - matches exactly 1 any character
163 * # - matches any digit, 0-9
164 * [list] - matches any character in (l, i, s and t in this example)
165 * [!list] - matches any character that isn't in the list
166 * Use \\* or \\? or \\[ to match a * or ? or [ respectively.
167 * To get a ] in a list, put it as first character of the list.
168 * To get a ! in a list, don't put it first.
169 * By common convention, special characters *, ? and # have no meaning when used in a list.
170 * If "case" is true, the matching is case sensitive.
171 * @author AceHart
172 * @link http://www.wc3c.net/showthread.php?t=102026
173 */
174 function StringMatch takes string text, string mask, boolean case returns boolean
175 local string a
176 local string b
177 local string x
178 local string y
179 local integer i = 0
180 local integer j = 0
181 local boolean m
182
183 if case then
184 set a = text
185 set b = mask
186 else
187 set a = StringCase(text, true)
188 set b = StringCase(mask, true)
189 endif
190
191 set x = SubString(a, 0, 1)
192 set y = SubString(b, 0, 1)
193 if x == null and y == null then
194 return false
195 endif
196
197 loop
198 if y == null then
199 return x == null
200 elseif y == "#" then
201 exitwhen not (x == "0" or x == "1" or x == "2" or x == "3" or x == "4" or x == "5" or x == "6" or x == "7" or x == "8" or x == "9")
202 elseif y == "\\" then
203 set j = j + 1
204 set y = SubString(b, j, j + 1)
205 exitwhen x != y
206 elseif y == "?" then
207 // nothing to do
208 elseif y == "[" then
209 set j = j + 1
210 set y = SubString(b, j, j + 1)
211 exitwhen y == null
212 set m = false
213 if y == "!" then
214 loop
215 set j = j + 1
216 set y = SubString(b, j, j + 1)
217 if y == null then
218 return false
219 endif
220 exitwhen y == "]"
221 if x == y then
222 set m = true
223 endif
224 endloop
225 exitwhen y != "]"
226 exitwhen m == true
227 else
228 loop
229 if y == null then
230 return false
231 endif
232 if x == y then
233 set m = true
234 endif
235 set j = j + 1
236 set y = SubString(b, j, j + 1)
237 exitwhen y == "]"
238 endloop
239 exitwhen y != "]"
240 exitwhen m == false
241 endif
242 elseif y == "*" then
243 loop
244 set j = j + 1
245 set y = SubString(b, j, j + 1)
246 exitwhen y != "*"
247 endloop
248 // if y == null then
249 if StringLength(y) < 1 then
250 return true
251 endif
252 set a = SubString(a, i, StringLength(a))
253 set b = SubString(b, j, StringLength(b))
254 loop
255 exitwhen a == null
256 if StringMatch(a, b, case) then
257 return true
258 endif
259 set i = i + 1
260 set a = SubString(a, 1, StringLength(a))
261 endloop
262 return false
263 else
264 exitwhen x != y
265 endif
266
267 set i = i + 1
268 set j = j + 1
269 set x = SubString(a, i, i + 1)
270 set y = SubString(b, j, j + 1)
271 endloop
272 return false
273 endfunction
274
275 endlibrary